package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.common.utils.bean.BeanUtils;
import com.lanyan.system.service.ICourierService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Building;
import com.lanyan.system.service.IBuildingService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 楼栋Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/building")
public class BuildingController extends BaseController
{
    @Autowired
    private IBuildingService buildingService;
    @Autowired
    private ICourierService courierService;

    /**
     * 查询楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "estateId", value = "所属楼栋", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Building building)
    {
        startPage();
        List<Building> list = buildingService.selectBuildingList(building);
        for (Building item : list) {
            item.setCourier(courierService.getById(item.getCourierId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:export')")
    @Log(title = "楼栋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Building building)
    {
        List<Building> list = buildingService.selectBuildingList(building);
        ExcelUtil<Building> util = new ExcelUtil<Building>(Building.class);
        util.exportExcel(response, list, "楼栋数据");
    }

    /**
     * 获取楼栋详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:building:query')")
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") Long buildingId)
    {
        return success(buildingService.selectBuildingByBuildingId(buildingId));
    }

    /**
     * 新增楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:add')")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "estateId", value = "所属楼栋", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult add(@RequestBody Building building)
    {
        List<Building> buildings = new ArrayList<>();
        Integer start = building.getStart();
        Integer end = building.getEnd();
        for (; start <= end; start++) {
            Building item = new Building();
            BeanUtils.copyBeanProp(item,building);
            item.setName(start+"号楼");
            buildings.add(item);
        }
        buildingService.saveBatch(buildings);
        return success();
    }

    /**
     * 修改楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:edit')")
    @Log(title = "楼栋", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "estateId", value = "所属楼栋", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "courierId", value = "所属配送员", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult edit(@RequestBody Building building)
    {
        return toAjax(buildingService.updateBuilding(building));
    }

    /**
     * 删除楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @Log(title = "楼栋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buildingIds}")
    public AjaxResult remove(@PathVariable Long[] buildingIds)
    {
        return toAjax(buildingService.deleteBuildingByBuildingIds(buildingIds));
    }
}
